package cn.baiweigang.uu.mybatis;

import cn.baiweigang.uu.utils.PropertiesUtil;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "cn.baiweigang.uu.db.mapper",sqlSessionFactoryRef = "sqlSessionFactoryBeanForSingle")
public class MybaticConfiguration {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBeanForSingle(@Qualifier(value = "singleDataSource")DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    @Bean(initMethod = "init")
    public DataSource singleDataSource(){
        String url = PropertiesUtil.getProperty("DB_URL");
        String userName = PropertiesUtil.getProperty("DB_USER_NAME");
        String pwd = PropertiesUtil.getProperty("DB_PWD");

        DruidDataSource druidDataSource=new DruidDataSource();
        druidDataSource.setName("bwgang");
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(pwd);

        return druidDataSource;
    }
}
